import { mapState, mapMutations } from 'vuex'
export default {
	name: 'msgBoard',
	data() {
		return {
			anonymity: false,
			msgList:[],
			message:"",
			name:"",
			loadingMsg:"更多留言加载中···",
			busy:false,
			pageSize:5,
			pageNum:1,
		}
	},
	computed: {
		...mapState([
			'token'
		]),
	},
	methods: {
		getMessageList(flag) {			
			this.$axios({
					method: 'get',
					url: '/msg/list?temp='+Math.random(),
					params:{pageSize:this.pageSize,pageNum:this.pageNum}
				})
				.then((res) => {
					if(res.data.status=="1"){
						if(flag) {
							this.msgList = this.msgList.concat(res.data.result.list);
							if(res.data.result.count == 0) {
								this.busy = true;
								this.loadingMsg = "已经是全部留言";
							} else {
								this.busy = false;
								this.loadingMsg = "更多留言加载中···";
							}
						} else {
							this.msgList = res.data.result.list;
							this.busy = false;
						}
					}
				})
				.catch((error) => {
					alert("请求异常")
				})
		},
		loadMore() {
			var that = this;
			this.busy = true
			setTimeout(function() {
				that.pageNum++
				that.getMessageList(true);
			}, 300)
		},
		submit(){
			if(this.message==""){
				alert("请输入留言内容")
				return false;
			}
			if(!this.anonymity&&this.name==""){
				alert("请输入您的昵称")
				return false;
			}
			this.$axios({
					method: 'post',
					url: '/msg/add?temp='+Math.random(),
					data:{message:this.message,name:this.name}
				})
				.then((res) => {	
					if(res.data.status=="1"){
						alert("发表成功")
						this.pageNum = 1;
						this.getMessageList();
					}					
				})
				.catch((error) => {
					alert("请求异常")
				})
		},
		del(id){
			this.$axios({
					method: 'post',
					url: '/msg/del?temp='+Math.random(),
					data:{id:id}
				})
				.then((res) => {
					if(res.data.status=="1"){
						this.pageNum = 1;
						alert("删除成功")
						this.getMessageList();
					}					
				})
				.catch((error) => {
					alert("请求异常")
				})
		}
	},
	components: {

	},
	created() {
		document.title = "Dolov 的留言板";
		this.getMessageList();
	}
}